import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
Vue.use(VueRouter)

// 封装this.$router.push和replace方法
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
VueRouter.prototype.push = function(location,res,rej){
    if(res && rej){
        originPush.call(this,location,res,rej)
    }else{
        originPush.call(this, location, ()=>{}, ()=>{})
    }
}
VueRouter.prototype.replace = function(location,res,rej){
    if(res && rej){
        originReplace.call(this,location,res,rej)
    }else{
        originReplace.call(this, location, ()=>{}, ()=>{})
    }
}

let router = new VueRouter({
    routes:[
        {
            path:'/home',
            name:'home',
            component: ()=>import('@/pages/Home'),
            meta:{isShow:true}
        },
        {
            path:'/login',
            name:'login',
            component: ()=>import('@/pages/Login'),
            meta:{isShow:false}
        },
        {
            path:'/register',
            name:'register',
            component: ()=>import('@/pages/Register'),
            meta:{isShow:false}
        },
        {
            // 使用:keyword占位，？表示可选可以不选
            path:'/search/:keyword?',
            name:'search',
            component: ()=>import('@/pages/Search'),
            meta:{isShow:true}
        },
        {
            path:'/',
            redirect:'/home',
        },
        {
            // 使用id占位， params参数
            path:'/detail/:skuId?',
            name:'detail',
            component:()=>import('@/pages/Detail')
        },
        {
            path:'/addcartSucess',
            name:'addcartSucess',
            component:()=> import('@/pages/AddCartSuccess')
        },
        {
            path:'/shopcart',
            name:'shopcart',
            component:()=>import('@/pages/ShopCart')
        },
        {
            path:'/trade',
            name:'trade',
            component:()=>import('@/pages/Trade')
        },
        {
            path:'/pay',
            name:'pay',
            component: ()=>import('@/pages/PayandList/Pay')
        },
        {
            path:'/paysuccess',
            name:'paysuccess',
            component: ()=>import('@/pages/PayandList/PaySuccess')
        },
        {
            path:'/center',
            name:'center',
            component: ()=>import('@/pages/PayandList/Center'),
            children:[
                {
                    path:'/myorder',
                    name:'myorder',
                    component:()=>import('@/pages/PayandList/Center/myOrder')
                },
                {
                    path:'/groupOrder',
                    name:'groupOrder',
                    component:()=>import('@/pages/PayandList/Center/groupOrder')
                }
            ]
        },
    ],
    // 滚动条行为
    scrollBehavior(to,from ,savedPosition){
        return {y:0}
    }
})

// 全局守卫 ，前置守卫
router.beforeEach(async (to,from,next)=>{
    // to：可以获取到即将跳转的路由信息
    // from：可以获取到从哪个路由跳转来的信息
    // next：  
    /*
        next()  直接放行
        next('/login') 放行到指定的路由
        next(false)  中断当前的导航，返回到from的路由
    */
   let token = store.state.user.token
   let userInfo = store.state.user.userinfo.name // 用来判断有没有当前对象
   if(token){
    //    已登录
        if(to.path==='/login' || to.path==='/register'){
            next('/')
            // console.log('已经登录过了，不需要重复登录');
        }else{
            // 判断有没有用户信息
            if(userInfo){
                next()
            }else{
                try {
                    await store.dispatch('getuserInfo')
                } catch (err) {
                    // 此时token过期了
                    store.dispatch('userlogout')
                    alert('您的登录信息已过期')
                    Vue.$router.push('/login')
                }
                next()
            }
        }
   }else{
    //    未登录
    // 未登录的时候，不能去交易，支付相关【pay，paysucess】，个人中心
    let topath = to.path
    if(to.path.indexOf('/pay')!=-1 ||to.path=='/trade' ||to.path.indexOf('/center')!==-1){
        next('/login?redirect='+topath)
    }else{
        next()
    }
   }
})

export default router